package com.chang.bootplus.controller;

import com.chang.bootplus.base.constant.Constant;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.RedisUtil;
import com.chang.bootplus.po.LogPO;
import com.chang.bootplus.service.ProductService;
import com.chang.bootplus.vo.LogVO;
import com.chang.bootplus.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/log")
@Api(tags = {"浏览记录"})
@CrossOrigin(origins = "*")
public class LogController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductService productService;

    @Autowired
    RedisUtil redisUtil;


    @PostMapping("/add")
    @ApiOperation(httpMethod = "POST", value = "添加浏览记录")
    public ApiResponse<Void> addlog(@RequestBody LogPO po){
        String ans=null;
        String key = Constant.Log_KEY+po.getUserId();
        Long val = po.getProductId();
        boolean b = redisUtil.hHasKey(key, val.toString());
        System.out.println(b);
        HashMap<String, Integer> map = new HashMap<>();
        if (!b){
            redisUtil.hset(key, val.toString(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), 604800);
            ans="记录成功";
            map.put("isLog",0);
            return new ApiResponse().setReMsg(ans).setBody(map);
        }else{
            ans="已记录过";
            map.put("isLog",1);
            return new ApiResponse().setReMsg(ans).setBody(map);
        }

    }


    @PostMapping("/delete")
    @ApiOperation(httpMethod = "POST", value = "删除浏览记录")
    public ApiResponse<Void> deletelog(@RequestBody LogPO po){
        String key = Constant.Log_KEY+po.getUserId();
        Long val = po.getProductId();
        redisUtil.hdel(key,val.toString());
        return new ApiResponse().setReMsg("删除成功");
    }

    @PostMapping("/list")
    @ApiOperation(httpMethod = "POST", value = "展示浏览记录，只需传UserId")
    public ApiResponse<ProductVO> listlog(@RequestBody LogPO po){

        String key = Constant.Log_KEY+po.getUserId();
        Map<Object, Object> hmget = redisUtil.hmget(key);
        Set<Object> pIds = hmget.keySet();
        LinkedList<Map.Entry<Object, Object>> entriesList = new LinkedList<>(hmget.entrySet());
        // 降序排序
        entriesList.sort((t1,t2)->{
            return -t1.getValue().toString().compareTo(t2.getValue().toString());
        });
        LinkedList<LogVO> logVOS = new LinkedList<>();
        entriesList.stream().forEach(t->{
            LogVO logVO = new LogVO();
            logVO.setPId(new Long(t.getKey().toString()).longValue());
            logVO.setAddTime(t.getValue().toString());
            logVOS.add(logVO);
        });
        return new ApiResponse(logVOS);
    }





}
